package cn.highset.modules.base.controller.api;

import cn.highset.base.ApiBaseController;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.utils.SecurityUtil;
import cn.highset.modules.base.entity.User;
import cn.highset.modules.base.entity.UserMiniprogram;
import cn.highset.modules.base.service.UserMiniprogramService;
import cn.highset.modules.base.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类 名 称：ApiAuthApiUserControllerController
 * 类 描 述：API用户信息
 * 创建时间：2019-06-15 10:32
 * 创 建 人：victor
 */
@Slf4j
@RestController
@Api(description = "API用户信息")
@RequestMapping("/api/user")
@Transactional
public class ApiUserController extends ApiBaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserMiniprogramService userMiniprogramService;

    /**
     * @name: 更新个人信息
     * @description: TODO
     * @return:
     * @date: 2019-06-15 10:41
     * @auther: victor
     */
    @ApiOperation(value = "更新个人信息")
    @PostMapping("update_user_miniprogram")
    public Object updateUserMiniprogram(@ModelAttribute UserMiniprogram userMiniprogram) {
        User user = securityUtil.getCurrUser();
        UserMiniprogram old = userMiniprogramService.findByUserid(user.getId());
        if (old == null) {
            old = new UserMiniprogram();
            old.setUserid(user.getId());
            old.setName(userMiniprogram.getName());
            old.setCompanyName(userMiniprogram.getCompanyName());
            old.setPhoneNumber(userMiniprogram.getPhoneNumber());
            old.setEmail(userMiniprogram.getEmail());
            userMiniprogramService.save(old);
        } else {
            old.setName(userMiniprogram.getName());
            old.setCompanyName(userMiniprogram.getCompanyName());
            old.setPhoneNumber(userMiniprogram.getPhoneNumber());
            old.setEmail(userMiniprogram.getEmail());
            userMiniprogramService.update(old);
        }
        redisTemplate.delete("user:miniprogram::" + user.getId());
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    /**
     * @name: 获取个人信息
     * @description: TODO
     * @return:
     * @date: 2019-06-15 10:41
     * @auther: victor
     */
    @ApiOperation(value = "获取个人信息")
    @PostMapping("get_user_miniprogram")
    public Object updateUserMiniprogram() {
        User user = securityUtil.getCurrUser();
        UserMiniprogram old = userMiniprogramService.findByUserid(user.getId());
        return new ResultUtil<Object>().setData(old);
    }

}
